package com.lhn.controller;

import com.lhn.entity.Order;
import com.lhn.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lhn
 * @date 2024-10-08 16:45
 */

@RestController
@RequestMapping("order")
public class OrderController {

    @Value("${server.port}")
    private String port;

    @GetMapping("index")
    public String index() {
        return "order index: " + port;
    }

    @Autowired
    private OrderService orderService;

    @PostMapping("save")
    public void save(@RequestBody Order order) {
        orderService.save(order);
    }

    @GetMapping("findAll/{index}/{limit}")
    public List<Order> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit) {
        return orderService.findAll(index, limit);
    }

    @GetMapping("findAllByUid/{uid}/{index}/{limit}")
    public List<Order> findAllByUid(
            @PathVariable("index") int index,
            @PathVariable("limit") int limit,
            @PathVariable("uid") int uid) {
        return orderService.findAllByUid(uid, index, limit);
    }

    @GetMapping("count")
    public int count() {
        return orderService.count();
    }

    @GetMapping("countByUid/{uid}")
    public int countByUid(@PathVariable("uid") int uid) {
        return orderService.countByUid(uid);
    }

    @GetMapping("findAllByState/{state}/{index}/{limit}")
    public List<Order> findAllByState(
            @PathVariable("state") int state,
            @PathVariable("index") int index,
            @PathVariable("limit") int limit) {
        return orderService.findAllByState(state, index, limit);
    }

    @GetMapping("countByState/{state}")
    public int countByState(@PathVariable("state") int state) {
        return orderService.countByState(state);
    }

    @PostMapping("updateState/{id}/{state}")
    public void updateState(@PathVariable("id") long id, @PathVariable("state") int state) {
        orderService.updateState(id, state);
    }

    @GetMapping("findAllState/{index}/{limit}")
    public List<Order> findAllState(
            @PathVariable("index") int index,
            @PathVariable("limit") int limit) {
        return orderService.findAllState( index, limit);
    }
}
